﻿using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using FollowMe.Infrastructure;
using System.ComponentModel.DataAnnotations.Schema;
using FollowMe.Model.Messages;

namespace FollowMe.Model
{
    public class Message : IAggregateRoot
    {
        [Key]
        [Column(Order = 1)]
        public Guid MessageId { get; internal set; }

        //[Key]
        [Required]
        [ForeignKey("MessageSender")]
        [Column(Order = 2)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid MessageSenderId { get; internal set; }

        //[Key]
        [Required]
        [ForeignKey("MessageReceiver")]
        [Column(Order = 3)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid MessageReceiverId { get; internal set; }

        [Required, MaxLength(3)]
        public string MessageType { get; internal set; }

        public string Content { get; internal set; }

        public double Lattitude { get; internal set; }

        public double Longitude { get; internal set; }

        [Required, DefaultValue(false)]
        public bool Read { get; internal set; }

        [Required]
        public DateTime Date { get; internal set; }


        public Member MessageSender { get; internal set; }

        public Member MessageReceiver { get; internal set; }

        
        public Message()
        {

        }

        //Leave private!  This delegates out the creation to the factory
        private Message(AMessage protoMessage) {
            //handle creation here!
            MessageId = Guid.NewGuid();
            MessageReceiverId = protoMessage.MessageReceiverId;
            MessageSenderId = protoMessage.MessageSenderId;
            Longitude = protoMessage.Longitude;
            MessageType = protoMessage.MessageType;
            Lattitude = protoMessage.Lattitude;
            Content = protoMessage.Content;
            Date = DateTime.Now;
            Read = false;
        }

        public static MessageFactory getFactory(){
            return new MessageFactory(m => new Message(m));
        }

        public Boolean isLocationMessage()
        {
            return (MessageType == "loc");
        }

        public Boolean isTextMessage()
        {
            return (MessageType == "txt");
        }

        public virtual void markAsRead()
        {
            Read = true;
        }

        protected virtual void Dispose(Boolean dis)
        {
            if (dis)
            {

            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
